package com.atguigu.spzx.feign.cart.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author LT
 * @create 2024-03-29-19:22
 */
@Component
public class AddTokenInterceptor implements RequestInterceptor {
    //因为购物车所有接口 都需要登录后才可以使用，并且需要使用登录拦截器透传的用户信息，所以需要配置请求头token

    @Override
    public void apply(RequestTemplate requestTemplate) {

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String token = request.getHeader("token");
        requestTemplate.header("token",token);


    }
}
